package com.fable.sp.core.service.impl;

import com.fable.sp.core.domain.Container;
import com.fable.sp.core.widget.ContainerContext;
import com.google.common.collect.Maps;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

/**
 * .
 *
 * @author stormning on 16/8/12.
 */
public class ContainerContextImpl implements ContainerContext {

    private Container container;

    private Map<String, String> attributes;

    public ContainerContextImpl(Container container) {
        this.container = container;
        if (container != null) {
            Map<String, String> attrs = container.getAttributes();
            this.attributes = attrs == null ? Collections.<String, String>emptyMap() : Maps.newHashMap(attrs);
        }
    }

    @Override
    public Long getId() {
        if (container == null) {
            return null;
        }
        return container.getId();
    }

    @Override
    public Container getContainer() {
        return container;
    }

    @Override
    public String getProperty(String name) {
        if (attributes == null) {
            return null;
        }
        return attributes.get(name);

    }

    @Override
    public Enumeration<String> getPropertyNames() {
        if (attributes == null) {
            return Collections.emptyEnumeration();
        }
        return Collections.enumeration(attributes.keySet());
    }

    @Override
    public String getContextInfo() {
        return container == null ? "" : container.getInfo();
    }

    @Override
    public Map<Long, Object> getInstanceFieldValues() {
        if (container == null) {
            return Collections.emptyMap();
        }
        Map<Long, Object> values = container.getInstanceColumnValues();
        if (values == null) {
            return Collections.emptyMap();
        }
        return values;
    }

    @Override
    public Map<String, Object> getColumnValues() {
        if (container == null) {
            return Collections.emptyMap();
        }
        Map<String, Object> values = container.getColumnValues();
        if (values == null) {
            return Collections.emptyMap();
        }
        return values;
    }
}
